package com.iseastar.guojiang.app;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.iseastar.guojiang.BeeApplication;
import com.iseastar.guojiang.newcabinet.DealTaskService;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import droid.frame.activity.ActivityMgr;
import droid.frame.utils.android.Log;
import droid.frame.utils.android.Notify;

/**
 * Created by joker on 2018/5/9.
 * 使用timer进行唤醒cpu进行活动
 */
public class TimerService extends Service {

    public static final String ACTION = "com.iseastar.guojiang.app.TimerService";
    private Timer timer = null;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Notify.getInstance().startForeground(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (timer == null) {
            timer = new Timer(true);
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.d("33333", "TimerService：onHandleIntent（）");
                Log.d("33333", ActivityMgr.isServiceRunning(BeeApplication.getContext(), "com.iseastar.guojiang.newcabinet.DealTaskService"));
                if (!ActivityMgr.isServiceRunning(BeeApplication.getContext(), "com.iseastar.guojiang.newcabinet.DealTaskService")) {
                    Intent intent = new Intent(getApplication(), DealTaskService.class);
                    intent.setPackage(getPackageName());
                    intent.putExtra("isStop", false);
                    startService(intent);
                    DealTaskService.startWorkLocation(false);
                }

            }
        }, new Date(), 10000);
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }
}
